/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Model.DAO;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nguyen Hung
 */
public class Product {

    private String productCode;
    private String cpu;
    private String hdd;
    private String display;
    private String OS;
    private String lanwirelessbluetooth;
    private String battery;
    private String weightheight;
    private double price;
    private String description;
    private String cardmonitor;
    private String vendor;
    private String image;
    private int warranty;
    private String DVDWL;
    private String ram;
    private int vendorid;
    private int quantity;

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getVendorid() {
        return vendorid;
    }

    public void setVendorid(int vendorid) {
        this.vendorid = vendorid;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public String getRam() {
        return ram;
    }

    public void setDVDWL(String DVDWL) {
        this.DVDWL = DVDWL;
    }

    public String getDVDWL() {
        return DVDWL;
    }

    public void setWarranty(int warranty) {
        this.warranty = warranty;
    }

    public int getWarranty() {
        return warranty;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getImage() {
        return image;
    }

    public void setCardmonitor(String cardmonitor) {
        this.cardmonitor = cardmonitor;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setVendor(String vendor) {
        this.vendor = vendor;
    }

    public void setWeightheight(String weightheight) {
        this.weightheight = weightheight;
    }

    public String getCardmonitor() {
        return cardmonitor;
    }

    public String getVendor() {
        return vendor;
    }

    public String getWeightheight() {
        return weightheight;
    }

    public String getDescription() {
        return description;
    }

    public Product() {
    }

    public Product(String productCode, String cpu, String ram, String hdd, String cardmonitor, String display, String image, String OS, String lanwirelessbluetooth, String battery, String weightheight, double price, int warranty, String vendor, String DVDWL, String description) {
        this.productCode = productCode;
        this.cpu = cpu;
        this.ram = ram;
        this.hdd = hdd;
        this.display = display;
        this.OS = OS;
        this.lanwirelessbluetooth = lanwirelessbluetooth;
        this.battery = battery;
        this.weightheight = weightheight;
        this.price = price;
        this.description = description;
        this.cardmonitor = cardmonitor;
        this.vendor = vendor;
        this.image = image;
        this.warranty = warranty;
        this.DVDWL = DVDWL;
    }

    public String getOS() {
        return OS;
    }

    public String getBattery() {
        return battery;
    }

    public String getCpu() {
        return cpu;
    }

    public String getDisplay() {
        return display;
    }

    public String getHdd() {
        return hdd;
    }

    public String getLanwirelessbluetooth() {
        return lanwirelessbluetooth;
    }

    public double getPrice() {
        return price;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setOS(String OS) {
        this.OS = OS;
    }

    public void setBattery(String battery) {
        this.battery = battery;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public void setDisplay(String display) {
        this.display = display;
    }

    public void setHdd(String hdd) {
        this.hdd = hdd;
    }

    public void setLanwirelessbluetooth(String lanwirelessbluetooth) {
        this.lanwirelessbluetooth = lanwirelessbluetooth;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public void save() {
        try {
            Connection connection = DAO.getConnection();
            String sql = "insert into Product"
                    + " values(?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?, ?, ?)";

            PreparedStatement ps2 = connection.prepareStatement(sql);
            ps2.setString(1, productCode);
            ps2.setString(2, image);
            ps2.setString(3, cpu);
            ps2.setString(4, ram);
            ps2.setString(5, hdd);
            ps2.setString(6, cardmonitor);
            ps2.setString(7, display);
            ps2.setString(8, lanwirelessbluetooth);
            ps2.setInt(9, warranty);
            ps2.setString(10, battery);
            ps2.setString(11, weightheight);
            ps2.setString(12, description);
            ps2.setInt(13, vendorid);
            ps2.setString(14, OS);
            ps2.setDouble(15, price);
            ps2.setString(16, DVDWL);
            ps2.setInt(17, quantity);
            ps2.execute();



        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void load(String productCode) {
        try {
            Connection connection = DAO.getConnection();
            String sql = "select * from product where productCode = ?";

            PreparedStatement ps = connection.prepareStatement(sql);
//            ps2.setString(1, productCode);
//            ps2.setString(2, image);
//            ps2.setString(3, cpu);
//            ps2.setString(4, ram);
//            ps2.setString(5, hdd);
//            ps2.setString(6, cardmonitor);
//            ps2.setString(7, display);
//            ps2.setString(8, lanwirelessbluetooth);
//            ps2.setInt(9, warranty);
//            ps2.setString(10, battery);
//            ps2.setString(11, weightheight);
//            ps2.setString(12, description);
//            ps2.setInt(13 , vendorid);
//            ps2.setString(14, OS);
//            ps2.setDouble(15, price);
//            ps2.setString(16, DVDWL);
//            ps2.setInt(17, quantity);
//            ps2.execute();
            ResultSet set = ps.executeQuery();
            while (set.next()) {
                this.cpu = set.getString("cpu");
//                this.ram = set.getString("ram");
//                this.hdd = hdd;
//                this.display = display;
//                this.OS = OS;
//                this.lanwirelessbluetooth = lanwirelessbluetooth;
//                this.battery = battery;
//                this.weightheight = weightheight;
//                this.price = price;
//                this.description = description;
//                this.cardmonitor = cardmonitor;
//                this.vendor = vendor;
//                this.image = image;
//                this.warranty = warranty;
//                this.DVDWL = DVDWL;
                price = set.getDouble("price");
            }



        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] arg) {
        Product p = new Product("U-15", "Intel Atom N450  1.66Ghz", "120Gb", "1 Gb", "X3150", "10.0” WXGA", "1 kg", "", "", "", "", 15000, 1, "MSI", "What what", "");
        p.setVendorid(11);
        p.save();
    }

    public void load() {
        try {
            Connection connection = DAO.getConnection();
            PreparedStatement statement = connection.prepareStatement("select *, vendorName from product, vendor where productCode = ? and vendor.vendorid = product.vendorid");
            statement.setString(1, productCode);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                image = rs.getString("image");
                cpu = rs.getString("cpu");
                ram = rs.getString("ram");
                hdd = rs.getString("hdd");
                cardmonitor = rs.getString("cardmonitor");
                display = rs.getString("display");
                lanwirelessbluetooth = rs.getString("wireless");
                warranty = rs.getInt("warranty");
                battery = rs.getString("battery");
                weightheight = rs.getString("weightheight");
                description = rs.getString("description");
                vendor = rs.getString("vendorName");
                price = rs.getDouble("price");
                OS = rs.getString("os");
                DVDWL = rs.getString("DVD-WL");
                quantity = rs.getInt("quantity");
                //                Product product = new Product(productCode, cpu, ram, hdd, cardmonitor, display, image, os, wireless, battery,wightheight , price,  warranty, vendorName,DVDWL , description);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

       public void updateImagesPath(String path) {
        Connection connection = DAO.getConnection();
        String sql = "update product set image = ? where productCode = ?";
        try {

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, path);
            ps.setString(2, productCode);
            ps.execute();
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
